__author__ = """blues zhao <blues@pingpower.cn>"""
__docformat__ = 'restructuredtext'

from AccessControl import ClassSecurityInfo
from zope.interface import implements

from Products.ATContentTypes.content.base import registerATCT
from Products.ATContentTypes.content.image import ATImage
from Products.ATContentTypes.content.base import ATCTFileContent
from Products.ATContentTypes.content import schemata
from Products.ATContentTypes.content.image import ATImageSchema
from Products.ePubBook.config import PROJECTNAME
from Products.ePubBook.interfaces import IPage

from Products.ePubBook import ePubBookMessageFactory as _
from Products.Archetypes import atapi
EpubPageSchema = ATImageSchema.copy()+atapi.Schema((
    atapi.StringField('directoryname',
                    searchable = True,
                    widget = atapi.StringWidget(
                        label = _(u'label_info_directoryname',default=u'Directory Name'),
                        description = _(u'help_info_directoryname',default=u''),
                        visible = {'edit' : 'visible', 'view' : 'invisible' }
                    )
    ),                                                           
))
schemata.finalizeATCTSchema(EpubPageSchema)

class EpubPage(ATImage):
    """A page, which can be displayed in an album."""

    portal_type    = 'EpubPage'
    archetype_name = 'EpubPage'
    _atct_newTypeFor = {'portal_type' : 'CMF Epub Page', 'meta_type' : 'Epub Page'}
    assocMimetypes = ('image/*', )
    assocFileExt   = ('jpg', 'jpeg', 'png', 'gif', )
    cmf_edit_kws   = ('file', )

    schema = EpubPageSchema
    implements(IPage)

    security       = ClassSecurityInfo()

    
    def __bobo_traverse__(self, REQUEST, name):
        """Transparent access to image scales
        """
        suffix = '.'+self.id
        if name.endswith(suffix):
            field = self.getField('image')
            image = None
            length = len(name) - len(suffix)
            scalename = name[:length]
            if scalename in field.getAvailableSizes(self):
                image = field.getScale(self, scale=scalename)
            if image is not None and not isinstance(image, basestring):
                # image might be None or '' for empty images
                return image

        return ATCTFileContent.__bobo_traverse__(self, REQUEST, name)

registerATCT(EpubPage, PROJECTNAME)